//
//  YBLoginViewModel.swift
//  Moonlight
//
//  Created by Penlon Kim on 2023/6/5.
//  Copyright © 2023 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit

class YBLoginViewModel: BaseViewModel {
    
    enum LoginRegisterType {
        case register
        case forgetPwd
    }
    
    lazy var loginModel = YBLoginModel()
    
    lazy var registerModel = YBRegisterModel()
    
}

// MARK: - 网络请求
extension YBLoginViewModel {
    
    /// 获取临时token
    func req_getToken(handler: @escaping BKResultHandler) {
        let path = NetApiPort.Login.getToken + "?code=\(SCHEME_CODE_TEST)"
        self.getReq(path: path, handler: handler)
    }
    
    /// 一键登录
    func req_clickLogin(token: String, handler: @escaping BKResultHandler) {
        let params = ["token": token]
        self.postReq(path: NetApiPort.Login.clickLogin, params: params, handler: handler)
    }
    
    /// 登录
    func req_login(_ params: [String: String], handler: @escaping BKResultHandler) {
        self.postReq(path: NetApiPort.Login.login, params: params, handler: handler)
    }
    
    /// 检查手机号能否注册
    func req_checkPhone(mobile: String, handler: @escaping BKResultHandler) {
        let path = NetApiPort.Register.checkPhone + "/\(mobile)"
        self.getReq(path: path, handler: handler)
    }
    
    /// 注册验证码
    func req_registerCode(mobile: String, handler: @escaping BKResultHandler) {
        let path = NetApiPort.SMS.registerCode + "/\(mobile)"
        self.getReq(path: path, handler: handler)
    }
    
    /// 注册
    func req_register(_ params: [String: String], handler: @escaping BKResultHandler) {
        self.postReq(path: NetApiPort.Register.register, params: params, handler: handler)
    }
    
    /// 忘记密码验证码
    func req_forgetPwdCode(mobile: String, handler: @escaping BKResultHandler) {
        let path = NetApiPort.SMS.forgetPwdCode + "/\(mobile)"
        self.getReq(path: path, handler: handler)
    }
    
    /// 忘记密码
    func req_forgetPwd(_ params: [String: String], handler: @escaping BKResultHandler) {
        self.postReq(path: NetApiPort.Login.forgetPwd, params: params, handler: handler)
    }
    
}

// MARK: - 参数验证
extension YBLoginViewModel {
    
    func validLoginParams(_ block: (String) -> Void) -> [String: String]? {
        var dic = [String: String]()
        
        guard loginModel.userName.isValid else {
            block("请输入手机号")
            return nil
        }
        guard loginModel.password.isValid else {
            block("请输入密码")
            return nil
        }
        
        dic["userName"] = loginModel.userName
        dic["password"] = loginModel.password
        dic["captcha"] = loginModel.captcha
        return dic
    }
    
    func validRegisterParams(type: LoginRegisterType, _ block: (String) -> Void) -> [String: String]? {
        var dic = [String: String]()
        
        guard registerModel.mobile.isValid, registerModel.mobile.isValidPhone() else {
            block("请输入手机号")
            return nil
        }
        guard registerModel.code.count > 0 else {
            block("请输入验证码")
            return nil
        }
        guard registerModel.password.isValid else {
            block("请输入密码")
            return nil
        }
        guard registerModel.password.isValidPassword() else {
            block("密码不合规")
            return nil
        }
        
        if type == .forgetPwd {
            guard registerModel.newPassword.isValid else {
                block("请输入新密码")
                return nil
            }
            if registerModel.password != registerModel.newPassword {
                block("两次密码输入不一致")
                return nil
            }
        }
        
        dic["mobile"] = registerModel.mobile
        dic["password"] = registerModel.password
        dic["code"] = registerModel.code
        dic["source"] = "gameIos"
        if type == .register {
            dic["company"] = registerModel.company
            dic["dept"] = registerModel.dept
            dic["position"] = registerModel.position
            dic["promoCode"] = registerModel.promoCode
        }
        return dic
    }
    
}
